/*****************************************************************************
*                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
******************************************************************************
*  @File Name        : SPI.c                                                 *
*  @Author           : ChipON AE/FAE Group                                   *
*  @Data$            : 2021-07-14                                            *
*  @Version          : V2.0                                                  *
*  @Description      : This file contains the spi configuration for KF32A156 *
*  devices                                                                   *
******************************************************************************
*  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
*  All rights reserved.                                                      *
*                                                                            *
*  This software is copyright protected and proprietary to                    *
*  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
******************************************************************************
*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
*                          REVISON HISTORY                                   *
*  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
*  Data       Version  Author        Description                             *
*  ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
*  2021-07-14 00.02.00  AE Group     Version 2.0 update                      *
*                                                                            *
*                                                                            *
*****************************************************************************/

/******************************************************************************
**                             Include Files                                **
******************************************************************************/
#include "SPI.h"

/*****************************************************************************
**                         Private Macro Definitions                        **
*****************************************************************************/
/*****************************************************************************
**                         Private Variables Definitions                    **
*****************************************************************************/
uint8_t Tx_Master[SPI_TRANSMISSION_DATA_SIZE] = "KF32A156-KungFu32 SPI Example master";
uint8_t Rx_Slave[SPI_TRANSMISSION_DATA_SIZE] = "KF32A156-KungFu32 SPI Example slaves";
uint8_t Spi_ReceiveData[SPI_TRANSMISSION_DATA_SIZE];
/*****************************************************************************
**                             Private Functions                            **
*****************************************************************************/

/*****************************************************************************
**                             Global Functions                            **
*****************************************************************************/
/**
 *  @brief: Initialize the spi module
 *  @param in: SPIx
 *  @param out : None
 *  @retval : None
 */
void Spi_Init(SPI_SFRmap *SPIx)
{
    SPI_InitTypeDef Spi_ConfigPtr;

    /* SPI mode */
    Spi_ConfigPtr.m_Mode = SPI_MODE_SLAVE;
    /* SPI clock */
    Spi_ConfigPtr.m_Clock = SPI_CLK_SCLK;
    /* Data transfer start control */
    Spi_ConfigPtr.m_FirstBit = SPI_FIRSTBIT_MSB;
    /* Spi idle state */
    Spi_ConfigPtr.m_CKP = SPI_CKP_LOW;
    /* Spi clock phase(Data shift edge) */
    Spi_ConfigPtr.m_CKE = SPI_CKE_1EDGE;
    /* Data width */
    Spi_ConfigPtr.m_DataSize = SPI_DATASIZE_8BITS;
    /* Baud rate :Fck_spi=Fck/2(m_BaudRate+1)*/
    Spi_ConfigPtr.m_BaudRate = 0x59;

    /* Spi reset */
    SPI_Reset(SPIx);
    /* Configure SPI module */
    SPI_Configuration(SPIx, &Spi_ConfigPtr);
//    SFR_SET_BIT_ASM(SPIx->CTLR, 7);

    /* Enable SPI module */
    SPI_Cmd(SPIx,TRUE);
}

/**
 *  @brief: Initialize the SPI IO
 *  @param in: None
 *  @param out : None
 *  @retval : None
 */
void Spi_IO_Init()
{
	 /* Configure SPI0 IO */
	/*
	 * SPI0_SCK = PB0
	 * SPI0_SDI = PB1
	 * SPI0_SDO = PB2
	 * SPI0_SS0 = PE5
	 */
	GPIO_Write_Mode_Bits(GPIOB_SFR, GPIO_PIN_MASK_2 | GPIO_PIN_MASK_1|GPIO_PIN_MASK_0, GPIO_MODE_RMP);
	GPIO_Write_Mode_Bits(GPIOE_SFR, GPIO_PIN_MASK_5, GPIO_MODE_RMP);

	GPIO_Pin_RMP_Config(GPIOB_SFR, GPIO_Pin_Num_2, GPIO_RMP_PG2_AF7);
	GPIO_Pin_RMP_Config(GPIOB_SFR, GPIO_Pin_Num_1, GPIO_RMP_PG2_AF7);
	GPIO_Pin_RMP_Config(GPIOB_SFR, GPIO_Pin_Num_0, GPIO_RMP_PG2_AF7);
	GPIO_Pin_RMP_Config(GPIOE_SFR, GPIO_Pin_Num_5, GPIO_RMP_PG2_AF7);
}
